<?php
/**
 * @author Inhere
 * @version v1.0
 * Use : form
 * Date : 2014-7-10
 */
namespace ulue\web\widgets;

use ulue\libs\front\Element;
use ulue\libs\front\Html;

class ModelField
{
    protected $type='text';

    protected $name;

    protected $attrs = [];

    static protected $model;

    protected $attrLabels = [];

    public function __construct($model, $name)
    {
        if ( ! self::$model ) {
            self::$model = $model;
        }

        $this->name = $name;
    }

    public function __toString()
    {
        $tagName = in_array($this->type, ['textarea', 'select']) ? $this->type : 'input';

        $field = Html::tag($tagName, $this->getValue(), []);
    }

    protected function model()
    {
        return self::$model;
    }

    protected function getValue()
    {
        return $this->model()->getAttr($name);
    }

    public function getLabelName($name)
    {
        if ( !$this->attrLabels ) {
            $this->attrLabels = $this->model->attrLabels();
        }

        return !empty($this->attrLabels[$name]) ? $this->attrLabels[$name] : ucfirst($name);
    }

    public function getModelName()
    {
        return basename(get_class($this->model));
    }

    public function labelTag($content)
    {
        return Html::tag('label', $content, ['id' => $this->getModelName().'-']);
    }

    public function text(array $attrs=[])
    {
        $this->type = 'text';
    }

    public function file($name, $label='', array $attrs=[])
    {
        $this->type = 'file';
    }

    public function submit($name, $label='', array $attrs=[])
    {
        $this->type = 'submit';
    }

    public function password($name, $label='', array $attrs=[])
    {
        $this->type = 'password';
    }

    public function textarea($name, $label='', array $attrs=[])
    {
        $this->type = 'textarea';
    }

    public function radio($name, $label='', array $attrs=[])
    {
        $this->type = 'radio';
    }

    public function radioList($name, array $data=[] , array $attrs=[])
    {
        $this->type = 'radio';
    }

    public function dropDownList($name, array $data=[] ,  array $attrs=[])
    {
        $this->type = 'select';
    }

    public function select($name, array $data=[] ,  array $attrs=[])
    {
        $this->type = 'select';
    }

    public function checkbox($name, $label='', array $attrs=[])
    {
        $this->type = 'checkbox';
    }

    public function checkboxList($name, array $data=[] , array $attrs=[])
    {
        $this->type = 'checkbox';
    }

}
